<template>
  <!--  授信合同  -->
    <div class="internalTransactionRecordsPage">
        <div class="search-wrap d-flex a-center flex-wrap">
            <div class="search-item d-flex a-center m-l20 m-b10">
                <label>起始日</label>
                <el-date-picker
                        class="m-l10"
                        v-model="form.dateTime"
                        value-format="YYYY-MM-DD"
                        type="daterange"
                        size="large"
                        range-separator="-"
                        start-placeholder="开始时间"
                        end-placeholder="结束时间"
                        @change="datePickerChange"
                />
            </div>
            <div class="search-item d-flex a-center m-l20 m-b10">
                <label>到期日</label>
                <el-date-picker
                        class="m-l10"
                        v-model="form.valueTime"
                        value-format="YYYY-MM-DD"
                        type="daterange"
                        size="large"
                        range-separator="-"
                        start-placeholder="开始时间"
                        end-placeholder="结束时间"
                        @change="valueTimePickerChange"
                />
            </div>
            <div class="search-item d-flex a-center m-l20 m-b10">
                <label>合同号</label>
                <el-input class="m-l10" v-model="form.contractNo" placeholder="请输入合同号"></el-input>
            </div>
            <div class="search-item d-flex a-center m-l20 m-b10">
                <label>用信单位</label>
                <el-input class="m-l10" v-model="form.creditCompany" placeholder="请输入用信单位"></el-input>
            </div>
            <div class="search-buttons m-l50 d-flex a-center m-b10">
                <el-button
                        color="#fc702b"
                        style="color:#fff;"
                        @click.prevent="onQuery"
                >查询</el-button>
                <el-button
                        color="#f0f0f0"
                        style="color: #000000"
                        @click.prevent="onReset"
                >重置</el-button>
                <el-divider direction="vertical" />
                <el-button
                        class="button_edit m-r10"
                        @click="onExport()"
                >
                    <svg-icon icon-class="project-detail-icon-derive" class="icon-export m-r5"></svg-icon>
                    一键导出
                </el-button>
                <el-button
                        class="button_edit"
                        @click="onImport()"
                >
                    <svg-icon icon-class="project-detail-icon-l-xiazai" class="icon-export m-r5"></svg-icon>
                    导入
                </el-button>
            </div>
        </div>
        <public-table
                ref="publicTableRef"
                class="m-t20"
                index
                radio
                :height="550"
                :indexWidth="'100'"
                :page="page"
                :tableData="tableData"
                :tableOption.sync="tableOption"
                @page-change="onPageChange"
                @handleSelectionChange="handleSelectionChange"
        >
        </public-table>
    </div>

    <import-file-popup
            :title="title"
            :is-tip="true"
            ref="importFilePopupRef"
            :interface-config="importFileInterfaceConfig"
            :template-config="templateConfig"
            @on-success="onQuery"
    />
</template>

<script setup>
import {onMounted, reactive, ref} from "vue";
import {
    ElSelect,
    ElOption,
    ElDivider,
    ElMessage,
    ElDatePicker, ElRadioGroup, ElRadioButton
} from "element-plus";
import PublicTable from "@/components/PublicTable.vue";

import {tableCommonFun} from "@/hooks/tableCommon";
import {bankManagementApi, companyApi} from "@/api/businessFinance";
import {useRouter} from "vue-router";
import ImportFilePopup from "@/components/importFilePopup.vue";
import router from "@/router";

const title = ref('导入CBS融资台账授信合同');
const interfaceConfig = reactive({
    fileName:'businessFinance',
    apiPath:'CBS.getFinancingContractList',
});
const importFileInterfaceConfig = reactive({
    fileName:'businessFinance',
    apiPath:'CBS.importFinancingContract',
});
const exportConfig = reactive({
    fileName:'businessFinance',
    apiPath:'CBS.getBankFinancingContractLeadingOut',
    tableName:'CBS融资台账授信合同导出'
});
const templateConfig = reactive({
    fileName:'businessFinance',
    apiPath:'CBS.downloadMB',
    param:{type:12},
    templateName:'CBS融资台账授信合同模版'
});

const {
    form,
    page,
    tableOption,
    tableData,
    selectedArr,
    onQuery,
    onReset,
    onPageChange,
    getData,
    handleSelectionChange,
    onExport
}  = tableCommonFun(interfaceConfig,exportConfig);

form.value = {
    contractNo:'',
    creditCompany:'',
    dateTime:'',
    startTime:'',
    endTime:'',
    startDate:'',
    endDate:'',
    valueTime:''
}

tableOption.value = [
    { label: "授信编号", prop: "trustNo", minWidth: 100 },
    { label: "合同号", prop: "contractNo", minWidth: 100 },
    { label: "额度类型", prop: "quotaType", minWidth: 100 },
    { label: "起始日", prop: "startDate", minWidth: 100 },
    { label: "到期日", prop: "stopDate", minWidth: 100 },
    { label: "单位名称", prop: "companyName", minWidth: 100 },
    { label: "金融机构", prop: "institutionName", minWidth: 100 },
    { label: "是否到期", prop: "isStopDate", minWidth: 100 },
    { label: "是否续期", prop: "renewal", minWidth: 100 },
    { label: "授信额度", prop: "creditAmount", minWidth: 100 },
    { label: "在用额度", prop: "useLimit", minWidth: 100 },
    { label: "可用额度", prop: "availableLimit", minWidth: 100 },
    { label: "累计已用额度", prop: "usedLimit", minWidth: 120 },
    { label: "是否循环", prop: "recycle", minWidth: 100 },
    { label: "用信单位", prop: "creditCompany", minWidth: 100 },
    { label: "担保方式", prop: "guaranteeType", minWidth: 100 },

];

const companyList = ref([]);
/**
 * 查公司列表
 */
const getCompanyList = () => {
    companyApi.getList({}).then(res=>{
        companyList.value = res.data;
    });
}

const visible = ref(false);

/**
 * 新增计息计算
 *
 */
const addHandle = () => {
    if(selectedArr.value.length===0){
        ElMessage.warning('请选择一条要操作的数据!');
        return;
    }
    visible.value = true;
}

/**
 * 起始日期
 */
const datePickerChange = (e) => {
    if(e===null || (e instanceof Array && e.length===0)){
        form.value.startTime = '';
        form.value.endTime = '';
        return;
    }
    form.value.startTime = e[0];
    form.value.endTime = e[1];
}

/**
 * 到期日期
 * @param e
 */
const valueTimePickerChange = (e) => {
    if(e===null || (e instanceof Array && e.length===0)){
        form.value.startDate = '';
        form.value.endDate = '';
        return;
    }
    form.value.startDate = e[0];
    form.value.endDate = e[1];
}

const importFilePopupRef = ref(null);

/**
 * 导入
 */
const onImport = () => {
    importFilePopupRef.value.openPopup();
}

/**
 *
 * @param type
 */
const toPath = (type) => {
    switch (type) {
        case 'tk': // 提款记录
            router.push({path:`/businessFinance/bankManagement/financingLedger/withdrawalRecords`});
            break;
        case 'hk': // 还款记录
            router.push({path:`/businessFinance/bankManagement/financingLedger/paymentHistory`});
            break;
        case 'sx': // 授信合同
            router.push({path:`/businessFinance/bankManagement/financingLedger/creditContract`});
            break;
    }
}

onMounted(()=>{
    getData({
        pageNum:1,
        pageSize:50
    });
});
</script>

<style lang="scss" scoped>
.internalTransactionRecordsPage{
  .search-wrap{
    .search-item{

      .el-input{
        width: auto !important;
      }
    }
    .search-buttons{
      .icon-export{
        width: 18px;
        height: 18px;
      }
    }
  }
}

.public-dialog-content{
  .dialog-title-box{
    height: 30px;
    span{
      font-size: 15px;
      font-weight: 600;
    }
  }
}
</style>
